<?php

namespace app\forms;

use app\components\validators\PositiveInteger;
use app\models\Field;
use app\components\helpers\MappingHelper;

class FieldForm extends Model
{
    public $id;
    public $module;
    public $type;
    public $formType;
    public $name;
    public $maxLength;
    public $defaultValue;
    public $value;
    public $tips;
    public $isValidate;
    public $isUnique;
    public $isCheck;
    public $isAllowEmpty;
    public $inListShow;
    public $inAddShow;
    public $status;
    public $sort;
    public $auth;

    public $page;
    public $pageSize;

    public function rules()
    {
        return [

            ['id', PositiveInteger::class],

            ['name', 'trim'],
            ['name', 'string', 'max' => 32],

            ['defaultValue', 'trim'],
            ['defaultValue', 'string', 'max' => 32],
            ['defaultValue', 'default', 'value' => ''],

            ['maxLength', 'integer'],
            ['maxLength', 'default', 'value' => 0],

            ['value', 'trim'],
            ['value', 'string', 'max' => 64],
            ['value', 'default', 'value' => ''],

            ['tips', 'trim'],
            ['tips', 'string', 'max' => 32],
            ['tips', 'default', 'value' => ''],

            ['module', 'integer'],
            ['module', 'in', 'range' => array_column(Field::getModuleMapping(), 'id')],

            ['type', 'integer'],
            ['type', 'in', 'range' => array_column(Field::getTypeMapping(), 'id')],

            ['formType', 'integer'],
            ['formType', 'in', 'range' => array_column(Field::getFormTypeMapping(), 'id')],

            ['isValidate', 'integer'],
            ['isValidate', 'in', 'range' => array_column(Field::getValidateMapping(), 'id')],

            ['isUnique', 'integer'],
            ['isUnique', 'in', 'range' => array_column(Field::getUniqueMapping(), 'id')],

            ['isCheck', 'integer'],
            ['isCheck', 'in', 'range' => array_column(Field::getCheckMapping(), 'id')],

            ['isAllowEmpty', 'integer'],
            ['isAllowEmpty', 'in', 'range' => array_column(Field::getAllowEmptyMapping(), 'id')],

            ['inListShow', 'integer'],
            ['inListShow', 'in', 'range' => array_column(Field::getListShowMapping(), 'id')],

            ['inAddShow', 'integer'],
            ['inAddShow', 'in', 'range' => array_column(Field::getAddShowMapping(), 'id')],

            ['status', 'integer'],
            ['status', 'in', 'range' => array_column(MappingHelper::statusList(), 'id')],
            ['status', 'default', 'value' => MappingHelper::STATUS_ACTIVE],

            ['auth', 'integer'],
            ['auth', 'default', 'value' => Field::AUTH_READ_AND_WRITE],

            ['sort', 'integer'],
            ['sort', 'default', 'value' => 0],

            ['name', 'required', 'on' => ['create', 'update']],
            ['module', 'required', 'on' => ['create', 'update']],
            ['type', 'required', 'on' => ['create', 'update']],
            ['formType', 'required', 'on' => ['create', 'update']],
            ['isValidate', 'required', 'on' => ['create', 'update']],
            ['isUnique', 'required', 'on' => ['create', 'update']],
            ['isCheck', 'required', 'on' => ['create', 'update']],
            ['isAllowEmpty', 'required', 'on' => ['create', 'update']],
            ['inListShow', 'required', 'on' => ['create', 'update']],
            ['inAddShow', 'required', 'on' => ['create', 'update']],

            [
                'id',
                'exist',
                'targetClass' => Field::class,
                'targetAttribute' => 'id',
                'on' => ['update', 'status', 'delete', 'auth', 'get-info']
            ],

            ['status', 'required', 'on' => ['status']],

            ['auth', 'required', 'on' => ['auth']],

            ['page', PositiveInteger::class],
            ['page', 'default', 'value' => 1, 'on' => ['index']],

            ['pageSize', PositiveInteger::class],
            ['pageSize', 'default', 'value' => 10, 'on' => ['index']],

        ];
    }

    public function scenarios()
    {
        return [
            'create' => [
                'module',
                'type',
                'formType',
                'name',
                'maxLength',
                'defaultValue',
                'value',
                'tips',
                'isValidate',
                'isUnique',
                'isCheck',
                'isAllowEmpty',
                'inListShow',
                'inAddShow',
                'status',
                'auth',
                'sort',
            ],
            'update' => [
                'id',
                'module',
                'type',
                'formType',
                'name',
                'maxLength',
                'defaultValue',
                'value',
                'tips',
                'isValidate',
                'isUnique',
                'isCheck',
                'isAllowEmpty',
                'inListShow',
                'inAddShow',
                'status',
                'auth',
                'sort',
            ],
            'delete' => ['id'],
            'get-info' => ['id'],
            'status' => ['id', 'status'],
            'auth' => ['id', 'auth'],
            'index' => ['module', 'page', 'pageSize']
        ];
    }
}